from django.shortcuts import render
from django.views import View
# Create your views here.
from DRF.serializers import BookSerializer
from demo_class.models import BookInfo, HeroInfo
from django.http import HttpResponse, JsonResponse
import json
from rest_framework.viewsets import ModelViewSet

'''
增加

POST  books/   btitle bpub_date   将保存的数据对象返回

GET    books/        获取多个数据资源

删除

DELETE  books/id        200 ok

修改  

PUT books/id    btitle bpub_date  将更新的数据对象返回

查询

GET  books/id     获取单一数据资源


'''


class Books(View):
    def get(self, request):
        '''
        获取多个数据资源
        :param request:
        :return:
        '''
        books = BookInfo.book_obj.all()

        book_list = []

        for book in books:
            book_list.append(
                {
                    'id': book.id,
                    'btitle': book.btitle,
                    'bread': book.bread,
                    'bcomment': book.bcomment
                }
            )

        return JsonResponse(book_list, safe=False)

    def post(self, request):
        '''
        保存数据对象

        :param request:
        :return:
        '''
        # 获取数据
        data = request.body
        data = data.decode()
        data_dict = json.loads(data)
        # 验证数据


        # 保存数据
        book = BookInfo.book_obj.create(btitle=data_dict['btitle'], bpub_date=data_dict['bpub_date'])

        # 结果返回

        return JsonResponse(
            {
                'id': book.id,
                'btitle': book.btitle,
                'bread': book.bread,
                'bcomment': book.bcomment
            }
        )


class Book(View):
    def get(self, request, pk):
        '''
        获取单一数据资源
        :param request:
        :return:
        '''
        try:
            book = BookInfo.book_obj.get(id=pk)
        except:
            return JsonResponse({'error': '数据不存在'}, status=400)

        return JsonResponse(
            {
                'id': book.id,
                'btitle': book.btitle,
                'bread': book.bread,
                'bcomment': book.bcomment
            }
        )

    def put(self, request, pk):
        '''
        更新数据对象
        :param request:
        :return:
        '''

        try:
            book = BookInfo.book_obj.get(id=pk)
        except:
            return JsonResponse({'error': '数据不存在'}, status=400)

        # 获取数据
        data = request.body
        data = data.decode()
        data_dict = json.loads(data)


        # 更新操作
        book.btitle = data_dict['btitle']
        book.bpub_date = data_dict['bpub_date']

        book.save()

        return JsonResponse(
            {
                'id': book.id,
                'btitle': book.btitle,
                'bread': book.bread,
                'bcomment': book.bcomment
            }
        )

    def delete(self, request, pk):
        '''
        删除
        :param request:
        :return:
        '''
        try:
            book = BookInfo.book_obj.get(id=pk)
        except:
            return JsonResponse({'error': '数据不存在'}, status=400)

        book.is_delete = True
        book.save()

        return JsonResponse({'message': 'ok'})


class BookViewSet(ModelViewSet):
    serializer_class = BookSerializer
    queryset = BookInfo.objects.all()